import { getSongDetail, getSongUrl, getLyric, getMvDetail, getMvVideo, getConnectMv } from '@/api/Personal'
import { getPhoneLogin, getUserSong } from '@/api/login'
import { RECEIVE_MUSIC_DATA, RECEIVE_MUSIC_MV, RECEIVE_USER_INFO } from './mutations-type'

export default {
  async getUserInfo({ commit }, payLoad) {
    let obj = {}
    let myList = []
    let subList = []
    console.log(payLoad)
    const { data: res } = await getPhoneLogin(payLoad)
    console.log(res)
    const { data: playlist } = await getUserSong(res.profile.userId)
    console.log(playlist)
    playlist.playlist.find(item => {
      if (item.userId === res.profile.userId) {
        myList.push(item)
      }
      else {
        subList.push(item)
      }
      obj.profile = res.profile
      obj.myList = myList
      obj.subList = subList
      window.sessionStorage.setItem('token', res.token)
      commit(RECEIVE_USER_INFO, obj)
    })
  },
  // 获取播放音乐信息
  async getAudioInfo({ commit }, payLoad) {
    // // 歌曲详情
    const { data: res } = await getSongDetail(payLoad.id)
    // // 播放音乐
    const { data: { data: play } } = await getSongUrl(payLoad.id)
    // // 获取歌词
    const { data: lyric } = await getLyric(payLoad.id)
    const obj = {}
    obj.info = res.songs[0]
    obj.url = play[0].url
    obj.lyric = lyric.lrc ? lyric.lrc.lyric : '没有歌词'
    commit(RECEIVE_MUSIC_DATA, obj)
  },
  // 获取mv详情信息
  async getMvDetail({ commit }, payLoad) {
    const { data: res } = await getMvDetail(payLoad)
    const { data: { data: video } } = await getMvVideo(payLoad)
    const { data: connect } = await getConnectMv(payLoad)
    const obj = {}
    obj.info = res.data
    obj.url = video.url
    obj.connect = connect.mvs
    commit(RECEIVE_MUSIC_MV, obj)
  }
}